package com.sunyur.tailor.channel.service.outer.keep.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.findsource.model.FindSourceFixedInforHeadModel;
import com.sunyur.tailor.channel.service.inner.findsource.model.FindSourceFixedInforItemModel;
import com.sunyur.tailor.channel.service.inner.findsource.param.FindSourceInforParam;
import com.sunyur.tailor.channel.service.inner.findsource.service.SunyurFindSourceService;
import com.sunyur.tailor.channel.service.inner.purchase.service.SunyurPurchaseApplyService;
import com.sunyur.tailor.channel.service.outer.keep.param.SRMPurchaseFeishuForApprovalParam;
import com.sunyur.tailor.channel.service.outer.keep.service.KeepToFeishuService;
import com.sunyur.tailor.channel.service.outer.keep.service.WorkflowBusinessStrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */

@Slf4j
@Service("tenderOrder")
public class TenderOrderStrategyImpl implements WorkflowBusinessStrategyService {

    @Autowired
    private SunyurFindSourceService service;

    @Autowired
    private SunyurClientUtil sunyurClient;
    @Value("${sunyur.api.findSourceFixedHead}")
    private String findSourceFixedHead;

    @Value("${sunyur.api.findSourceProjectList}")
    private String findSourceProjectList;

    @Value("${feishu.tenderOrder.approvalCode}")
    private String approvalCode;

    @Autowired
    private KeepToFeishuService keepToFeishuService;


    @Autowired
    private SunyurPurchaseApplyService sunyurPurchaseApplyService;

    @Autowired
    private InquireOrderStrategyImpl inquireOrderStrategy;



    @Override
    public ChannelResult start(MsgResultCode msgResultCode) {

      return  inquireOrderStrategy.start(msgResultCode);


//        log.info("tenderOrder start  msgResultCode:{}", JSONObject.toJSONString(msgResultCode));
//        if(msgResultCode.getAction().equals("fixed")||msgResultCode.getAction().equals("Fixed")||msgResultCode.getAction().equals("FIXED")||msgResultCode.getAction().equals("commit"))
//        {
//
//
//            SunyurResult sunyurResult =
//                    sunyurClient.execPost(findSourceProjectList,
//                            JSON.toJSONString(
//                                    MapUtil.builder()
//                                            .put("purCode", msgResultCode.getBusinessCode())
//                                            .put("type", 2).build()), null);
//            log.info("sunyurResult: {}", JSON.toJSONString(sunyurResult));
//            // 主线报错
//            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
//                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
//            }
//            List<Map<String, Object>> content = (List<Map<String, Object>>) sunyurResult.getContent();
//            Long code = Long.valueOf(content.get(0).get("code").toString());
//            log.info("code: {}", code);
//
//
//            try {
//                ChannelResult<FindSourceFixedInforHeadModel> findSourceFixedInforHeadModelChannelResult = service.searchFixedInforHead(new FindSourceInforParam(code));
//                if (!ChannelResultUtil.isSuccess(findSourceFixedInforHeadModelChannelResult)) {
//                    return findSourceFixedInforHeadModelChannelResult;
//                }
//                ChannelListResult<FindSourceFixedInforItemModel> findSourceFixedInforItemModelChannelListResult = service.searchFixedInforItem(new FindSourceInforParam(code));
//                if (!ChannelResultUtil.isSuccess(findSourceFixedInforItemModelChannelListResult)) {
//                    return findSourceFixedInforItemModelChannelListResult;
//                }
//
//                log.info("findSourceFixedInforHeadModelChannelResult: {}", JSON.toJSONString(findSourceFixedInforHeadModelChannelResult));
//                log.info("findSourceFixedInforItemModelChannelListResult: {}", JSON.toJSONString(findSourceFixedInforItemModelChannelListResult));
//
//
//                SRMPurchaseFeishuForApprovalParam srmPurchaseFeishuForApprovalParam = new SRMPurchaseFeishuForApprovalParam();
//                //拼装请求参数
//                srmPurchaseFeishuForApprovalParam.setApproval_code(approvalCode); //固定值
//                srmPurchaseFeishuForApprovalParam.setUser_id(findSourceFixedInforHeadModelChannelResult.getContent().getAgentWorkNum()); //申请人id
//                srmPurchaseFeishuForApprovalParam.setOpen_id(findSourceFixedInforHeadModelChannelResult.getContent().getAgentWorkNum()); //PR头/content/proposerCode
//                ArrayList formList = new ArrayList();
//
//
//
//
//
//            }catch (Exception e)
//            {
//                log.error("TenderOrderStrategyImpl exception", e);
//                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), e.getMessage());
//            }
//
//
//
//
//            return null;
//        }
//
//        return null;
        }

}
